<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2006 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Store a tag definition.
 */
class CMS_Tag extends Pluf_Model
{
    function init()
    {
        $this->_a['table'] = 'CMS_tags';
        $this->_a['model'] = 'CMS_Tag';
        $this->_a['cols'] = array(
                            'id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Sequence',
                                  'blank' => true, 
                                  ),
                            'website' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'CMS_Website',
                                  'blank' => false,
                                  'verbose' => __('website'),
                                  ),
                            'class' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'default' => 'other',
                                  'verbose' => __('tag class'),
                                  'help_text' => __('The class of the tag.'),
                                  ),
                            'name' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'verbose' => __('name'),
                                  ),
                            'lcname' => 
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'editable' => false,
                                  'verbose' => __('lcname'),
                                  'help_text' => __('Lower case version of the name for fast searching.'),
                                  ),
                            );

        $this->_a['idx'] =  array(
                                  'lcname_idx' =>
                                  array(
                                        'col' => 'lcname',
                                        'type' => 'normal',
                                        ),
                                  'class_idx' =>
                                  array(
                                        'col' => 'class',
                                        'type' => 'normal',
                                        ),
                                  'website_idx' =>
                                  array(
                                        'col' => 'website',
                                        'type' => 'normal',
                                        ),
                                  );
    }

    function preSave()
    {
        $this->lcname = mb_strtolower($this->name);
    }

    /**
     * Find all the tags not associated to a post and remove them.
     */
    public static function removeUnusedTags()
    {
        $gtag = new CMS_Tag();
        $db =& Pluf::db($gtag);
        $table = $gtag->getSqlTable();
        $sql = 'SELECT id FROM '.$table.' LEFT OUTER JOIN '
            .$db->pfx.'cms_post_cms_tag_assoc '
            .'ON (id=cms_tag_id) WHERE cms_tag_id IS NULL';    
        $res = $db->select($sql);
        $delete = 'DELETE FROM '.$table.' WHERE id IN (%s)';
        $ids = array();
        foreach ($res as $id) {
            $ids[] = (int) $id['id'];
        }
        $db->execute(sprintf($delete, implode(', ', $ids)));
    }

    /**
     * Add a tag if not already existing.
     */
    public static function addTag($website, $class, $name)
    {
        $class = trim($class);
        $name = trim($name);
        $website = trim($website);
        $gtag = new CM_Tag();
        $sql = new Pluf_SQL('website=%s AND class=%s AND lcname=%s', 
                            array($website, $class, mb_strtolower($name)));
        $tags = $gtag->getList(array('filter' => $sql->gen()));
        if ($tags->count() == 0) {
            // create a new tag
            $tag = new CM_Tag();
            $tag->website = $website;
            $tag->name = $name;
            $tag->class = $class;
            $tag->create();
        }
    }

    function __toString()
    {
        return $this->class.':'.$this->name;
    }

}
